var app = getApp()
Page({
  data: {
    region: ['请选择', '请选择', '请选择'],
    customItem: '全部',
    name: '',
    bancard: '',
    idcard: '',
    phone: '',
    code: '',
    text: '获取验证码',
    currentTime: 61, //倒计时
    disabled: false, //按钮是否禁用
    isChecked: false,
    status: 0,
    mask: false,
    id: '',
    data: ''
  },
  // formid
  formSubmit: function(e) {
    var formId = e.detail.formId
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function(res) {
        wx.request({
          url: app.url.requestUrl + '/user/collect_formid',
          method: 'POST',
          data: {
            openid: res.data.openid,
            token: res.data.token,
            formid: formId
          },
          success: function(res) {
            if (res.data.code == 1) {

            }
          }
        })
      }
    })
  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      area: e.detail.value.join(''),
      region: e.detail.value
    })
  },
  // 姓名
  valName(e) {
    this.setData({

      name: e.detail.value
    })
  },
  // 银行卡卡号
  valBankCard(e) {
    this.setData({
      bancard: e.detail.value
    })

  },
  // 身份证
  valIdcard(e) {
    this.setData({
      idcard: e.detail.value
    })
  },
  // 手机
  valPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 验证码
  valCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  changeSwitch: function(e) {
    this.setData({
      isChecked: e.detail.value
    })
    if (this.data.isChecked == false) {
      this.setData({
        status: 0
      })
    } else {
      this.setData({
        status: 1
      })
    }

  },
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function(res) {
        wx.request({
          url: app.url.requestUrl + '/personal/bank_details ',
          method: 'POST',
          data: {
            openid: res.data.openid,
            token: res.data.token,
            bank_id: options.id
          },
          success: function(res) {
            if (res.data.code == 1) {
              that.setData({
                data: res.data.data,
                name: res.data.data.realname,
                phone: res.data.data.mobile,
                bancard: res.data.data.bankcard,
                idcard: res.data.data.idcard
              })
            }

          }
        })
      }
    })

  },
  submitInfos: function() {
    var compare = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var that = this;
    if (that.data.name.length == 0 || that.data.phone.length == 0) {
      wx.showToast({
        title: '用户名不能为空',
        icon: 'none',
        image: '',
        duration: 1000
      })
    } else if (that.data.phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误',
        icon: 'none',
        image: '',
        duration: 1000
      })
      return false;
    } else if (!compare.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none',
        image: '',
        duration: 1000
      })
      return false;
    } else if (!that.data.bancard) {
      wx.showToast({
        title: '银行卡号不能为空',
        icon: 'none',
        image: '',
        duration: 1000
      })
    } else if (!that.data.idcard) {
      wx.showToast({
        title: '身份证号码不能为空',
        icon: 'none',
        image: '',
        duration: 1000
      })

    } else if (!that.data.code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        image: '',
        duration: 1000
      })

    } else {
      //TODO post data to sever
    }
    wx.getStorage({
      key: 'user',
      success: function(res) {
        wx.request({
          url: app.url.requestUrl + '/personal/bankbind',
          method: 'POST',
          data: {
            openid: res.data.openid,
            token: res.data.token,
            realname: that.data.name,
            mobile: that.data.phone,
            idcard: that.data.idcard,
            bankcard: that.data.bancard,
            code: that.data.code,
            id: that.data.id

          },
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            console.log(res.data.data.set_pwd)
            if (res.data.code == 1) {
              wx.showToast({
                title: res.data.msg,
                icon: '',
                duration: 2000,
                mask: true
              })
              wx.navigateTo({
                url: `../my-psd/my-psd?set_pwd=${res.data.data.set_pwd}`,
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                image: '',
                duration: 1000
              })
            }
            // wx.showToast({
            //   title: res.msg,
            //   icon: 'none',
            //   image: '',
            //   duration: 1000
            // })
            console.log(res)
            // wx.navigateBack({
            //   delta: 1
            // })
          }
        })
      }
    })
  },
  getcode: function() {
    var that = this;
    that.setData({
      disabled: true, //只要点击了按钮就让按钮禁用 （避免正常情况下多次触发定时器事件）
      color: '#ccc',
    })

    var phone = that.data.phone;
    var currentTime = that.data.currentTime //把手机号跟倒计时值变例成js值
    var warn = null; //warn为当手机号为空或格式不正确时提示用户的文字，默认为空

    if (phone == '') {

      warn = "号码不能为空";

    } else if (phone.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone)) {

      warn = "手机号格式不正确";

    } else {

      //当手机号正确的时候提示用户短信验证码已经发送

      wx.getStorage({
        key: 'user',
        success: function(res) {
          wx.request({
            url: app.url.requestUrl + '/message/sendcode',
            method: 'POST',
            data: {
              openid: res.data.openid,
              token: res.data.token,
              mobile: that.data.phone,
            },
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              console.log(res)
              wx.showToast({

                title: '短信验证码已发送',

                icon: 'none',

                duration: 2000

              });
            }
          })
        }
      })





      //设置一分钟的倒计时

      var interval = setInterval(function() {

        currentTime--; //每执行一次让倒计时秒数减一

        that.setData({
          text: currentTime + 's', //按钮文字变成倒计时对应秒数
        })
        //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字

        if (currentTime <= 0) { 
          clearInterval(interval)

          that.setData({

            text: '重新发送',

            currentTime: 61,

            disabled: false,

            color: '#929fff'

          })

        }

      }, 1000);


    };


    if (warn != null) {

      wx.showModal({

        title: '提示',

        content: warn

      })
      that.setData({

        disabled: false,

        color: '#929fff'

      })

      return;
    }

  }
})